package git.soulbgm.common;

import git.soulbgm.enumeration.GroupType;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 分组值
 *
 * @author SoulBGM
 * @date 2021/12/21 22:47
 */
public class GroupValue implements Serializable {

    private GroupType type;

    private AtomicLong value;

    public GroupValue(GroupType type, Number value) {
        this.type = type;
        if (type == GroupType.COUNT) {
            this.value = new AtomicLong(1);
        } else {
            this.value = new AtomicLong(value.longValue());
        }
    }

    public void change(Number value, boolean isGroup) {
        switch (type) {
            case MAX:
                if (this.value.longValue() < value.longValue()) {
                    this.value.set(value.longValue());
                }
                break;
            case MIN:
                if (this.value.longValue() > value.longValue()) {
                    this.value.set(value.longValue());
                }
                break;
            case SUM:
                this.value.addAndGet(value.longValue());
                break;
            default:
                this.value.addAndGet(isGroup ? value.longValue() : 1);
                break;
        }
    }

    public void change(Number value) {
        change(value, false);
    }

    public GroupType getType() {
        return type;
    }

    public void setType(GroupType type) {
        this.type = type;
    }

    public Number getValue() {
        return value.longValue();
    }

    public void setValue(Number value) {
        this.value = new AtomicLong(value.longValue());
    }
}
